package de.tuberlin.julia.SFD_Numbers;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import com.google.code.microlog4android.Logger;
import com.google.code.microlog4android.LoggerFactory;

import experiment.ApplyDesign;
import experiment.util.ExpConfigParsing;
import experiment.util.ExpParsedConfigDataSet;

import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class NumberView extends Activity {
	
	private static final Logger logger = LoggerFactory.getLogger();	
	
	private ExpParsedConfigDataSet expDataSet;	
	private ApplyDesign applyDesign;
	
	TextView txtView;	
	MediaPlayer mp;
	int i=0;
	int j = 4;
	int k=0;
	Intent myIntent;
	
    // Datum
    TimeZone tz = TimeZone.getTimeZone("Europe/Berlin");
    Locale locale = Locale.GERMANY;
    final Calendar c = Calendar.getInstance(tz,locale);
    
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
   
        
        SimpleDateFormat f= new SimpleDateFormat("dd.MM.yyyy hh:mm");
        f.setTimeZone(c.getTimeZone());
        Date date = c.getTime();

        logger.info("* " + f.format(date) + " start");
        	
		setContentView(R.layout.main);		
		
		expDataSet = new ExpParsedConfigDataSet();

		new ExpConfigParsing(this,this.getIntent().getStringExtra("vpStr"), expDataSet);
		
		applyDesign = new ApplyDesign(expDataSet, this); 
	}
	
	public void onResume(){		
		super.onResume();
		txtView = (TextView) findViewById(R.id.TextViewNumber);
		
		txtView.setBackgroundColor(getResources().getColor(R.color.black));	
		txtView.setTextColor(applyDesign.standardColor);
		
        /*
		SimpleDateFormat f= new SimpleDateFormat("dd.MM.yyyy hh:mm");
        f.setTimeZone(c.getTimeZone());
        Date date = c.getTime();
        
    	logger.info("Start des " + k+1+ ". Blocks." + f.format(date));
    	*/
		
		if(j%404==0)
		{
			j=4;
			i=0;
			k++;
			myIntent = new Intent(this,EndBlockView.class);
            myIntent.putExtra("block", k);
            myIntent.putExtra("run", 0);
			this.startActivityForResult(myIntent,0);
			return;
		}		
		j++; 
		if(j%5==0)
		{
			i++;
			txtView.setText("PIN "+ i);
			logger.info("PIN " + i);
		}
		

		

		
		prepareButtons(applyDesign.getFeedbackType());
	}
	
	private void prepareButtons(String modality) {
		OnClickListener listener;
		if(modality.equals("visual"))
			listener = new VisualBtnPressedListener(this, applyDesign);
		else if(modality.equals("auditiv")){
			prepareMP();
			listener = new AudioBtnPressedListener(applyDesign, mp);
		}
		else if(modality.equals("tactile"))
			listener = new TactileBtnPressedListener(this, applyDesign);
		else if(modality.equals("multimodal")){
			prepareMP();
			listener = new MultiBtnPressedListener(this, applyDesign, mp);
		}
		else if(modality.equals("auditivVisual")){
			prepareMP();
			listener = new AudioVisualBtnPressedListener(this, applyDesign, mp);
		}
		else if(modality.equals("auditivTactile")){
			prepareMP();
			listener = new AudioTactileBtnPressedListener(this, applyDesign, mp);
		}
		else if(modality.equals("visualTactile")){
			listener = new VisualTactileBtnPressedListener(this, applyDesign, mp);
		}
		else if(modality.equals("noneTrigger")){
			listener = new ButtonPressedListenerNoneTrigger(this, applyDesign);
		}
		else if(modality.equals("enter")){
			listener = new EnterBtnPressed(this, applyDesign);
		}
		else
			listener = new ButtonPressedListener(this, applyDesign);
		
		styleAndConfigButton((TextView) findViewById(R.id.TextView00), listener);
		styleAndConfigButton((TextView) findViewById(R.id.TextView01), listener);
		styleAndConfigButton((TextView) findViewById(R.id.TextView02), listener);
		styleAndConfigButton((TextView) findViewById(R.id.TextView03), listener);
		styleAndConfigButton((TextView) findViewById(R.id.TextView04), listener);
		styleAndConfigButton((TextView) findViewById(R.id.TextView05), listener);
		styleAndConfigButton((TextView) findViewById(R.id.TextView06), listener);
		styleAndConfigButton((TextView) findViewById(R.id.TextView07), listener);
		styleAndConfigButton((TextView) findViewById(R.id.TextView08), listener);
		styleAndConfigButton((TextView) findViewById(R.id.TextView09), listener);
		styleAndConfigButton((TextView) findViewById(R.id.TextViewStar), listener);
		styleAndConfigButton((TextView) findViewById(R.id.TextViewHash), listener);
	}
	
	private void prepareMP(){
			
		if (mp != null){
			mp.release();
		}
		mp = MediaPlayer.create(this, R.raw.start);
		
		mp.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {					
				
			}
		});				
		mp.setOnErrorListener(new OnErrorListener() {

			@Override
			public boolean onError(MediaPlayer mp, int arg1, int arg2) {
				mp.release();
				return false;
			}
		});
	}
	
	private void styleAndConfigButton(TextView tv, OnClickListener listener) {
		tv.setOnClickListener(listener);
		tv.setTextColor(new ColorStateList(new int[][]{new int[]{android.R.attr.state_pressed}, new int[0]}, new int[]{Color.WHITE, Color.WHITE}));
		GradientDrawable gradientDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{Color.LTGRAY, Color.DKGRAY});
		gradientDrawable.setCornerRadius(10);
		tv.setBackgroundDrawable(gradientDrawable);
	}

}
